package com.bjqh.edu.UI.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;


import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Base.Connect;
import com.bjqh.edu.Base.OrderPopupWindow;
import com.bjqh.edu.Bean.Course;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.Utils.PermissionUtil;
import com.bjqh.edu.adapter.CoursePurchaseAdapter;
import com.bjqh.edu.adapter.CoursePurchaseAdapter2;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/9/15.
 */

public class TwoDayFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String gradeid;
    private Activity mActivity;

    @BindView(R.id.rec_gh)
    ListView recGh;
    Unbinder unbinder;
    private CoursePurchaseAdapter2 adapter;
    private List<Course.RowsBean> list;
    private OrderPopupWindow orderPopupWindow;
    private View v;
    private Connect connect;

    public static TwoDayFragment newInstance(Connect connect,String param1) {
        TwoDayFragment fragment = new TwoDayFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putParcelable(ARG_PARAM2,connect);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            connect = getArguments().getParcelable(ARG_PARAM2);
            gradeid =  getArguments().getString(ARG_PARAM1);
            mActivity = connect.getActivity();
        }

    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one_day, null);
        ButterKnife.bind(this, view);
        unbinder = ButterKnife.bind(this, view);
        v= (View)getActivity().findViewById(R.id.tv_title);

        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(v.getTag(R.id.tv_title)!=null)
        {


            Log.e("ll",gradeid);
            OkGo.init(getActivity().getApplication());
            OkGo.post(AppConstants.BASE_URL + "/course/datalist.shtml")
                    .tag(this)
                    .cacheKey("cacheKey")
                    .cacheMode(CacheMode.DEFAULT)
                    .params("gradeid", gradeid)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            Gson gson = new Gson();
                            Course course = gson.fromJson(s, Course.class);

                            if(list==null)
                            {
                                list=course.getRows();
                                Log.e("活动数据",s);
                                adapter = new CoursePurchaseAdapter2(mActivity, list);
                                recGh.setAdapter(adapter);


                            }
                            else {
                                list.clear();
                                list.addAll(course.getRows());
                                adapter.setList(list);
                                recGh.setAdapter(adapter);
                            }


                        }
                    });
        }


    }
    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener(){

        public void onClick(View v) {
            orderPopupWindow.dismiss();
            switch (v.getId()) {
                case R.id.btn_payment:
                    //  Toast.makeText(getActivity(), "支付", Toast.LENGTH_SHORT).show();
                    if (PermissionUtil.hasCameraPermission(getActivity())) {

                    }
                    break;
                default:
                    break;
            }

        }

    };

}
